package cn.vinann.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.vinann.demo.annotation.Desensitize;
import cn.vinann.demo.domain.akuc.Menu;
import cn.vinann.demo.domain.akweb.ItbUser;
import cn.vinann.demo.dto.UserDto;
import cn.vinann.demo.repository.akuc.MenuRepository;
import cn.vinann.demo.repository.akweb.ItbUserRepository;

/**
 * 数据操作
 * @author wn
 *
 */
@RestController
@RequestMapping("/data")
public class DataSourceController {

	@Autowired
	private ItbUserRepository itbUserRepository;
	
	@Autowired
	private MenuRepository menuRepository;
	
	@Desensitize("realName")
	@RequestMapping("/users")
	public List<ItbUser> users(){
		ItbUser itbUser = new ItbUser();
		itbUser.setRealName("SpringBoot");
		itbUser = itbUserRepository.save(itbUser);
		itbUser.setRealName("NewSpringBoot");
		itbUser = itbUserRepository.save(itbUser);
		itbUserRepository.delete(itbUser);
		return itbUserRepository.findAll();
	}
	
	@RequestMapping("/userDto")
	public List<UserDto> userDto(){
		return itbUserRepository.findCountGroupByName();
	}
	
	@RequestMapping("/menus")
	public List<Menu> menus(){
		return menuRepository.findAll();
	}
	
	@Transactional
	@RequestMapping("/transactional")
	public String Transactional(){
		
		ItbUser itbUser = new ItbUser();
		itbUser.setRealName("SpringBoot");
		itbUser = itbUserRepository.save(itbUser);
		
		Menu menu = new Menu();
		menu.setName("menu");
		menuRepository.save(menu);
		
		"1".substring(10);
		return "事务";
	}
	
	
}
